package softtech.apps.callblocker.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import softtech.apps.callblocker.R;
import softtech.apps.callblocker.adapter.AdapterMessengerHistory.ViewHolder;
import softtech.apps.callblocker.model.HeaderSMSBlockedItem;
import softtech.apps.callblocker.model.SectionStructure;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AdapterCallHistory extends BaseAdapter{

	ArrayList<HeaderSMSBlockedItem> CallListHeader = new ArrayList<HeaderSMSBlockedItem>();
	LayoutInflater inf;
	Context context;
	//display date
    LinearLayout.LayoutParams lparamDate;
    //display date and number
    LinearLayout.LayoutParams lparamDateNumber ;//= new  LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
    
	public AdapterCallHistory(ArrayList<HeaderSMSBlockedItem> CallListHeader,LayoutInflater inf
			,Context context){
		this.CallListHeader = CallListHeader;
		this.inf = inf;
		this.context = context;
	}
/**
* display 30 item
*/
public int count =30;

	@Override
     public int getCount() {
            // TODO Auto-generated method stub
            //return sectionList.size();
		if(CallListHeader.size()>=count)
		{
			return count;
		}
		if(CallListHeader.size()<=0){
			return 1;
		}
		return CallListHeader.size();
     }


     @Override
     public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
     }
	@Override
     public View getView(int position, View vi, ViewGroup arg2) {
		if (CallListHeader == null || CallListHeader.equals(null)
				|| CallListHeader.size() == 0) {
			vi = inf.inflate(R.layout.item_search_notfound,
					null);
			return vi;
		}
		ViewHolder viewholder;
            if(vi==null)
            {
            vi = inf.inflate(R.layout.item_call_history_date, null);
            viewholder= new ViewHolder();
            //create component
            fCreateComponent(vi,viewholder);
            vi.setTag(viewholder);
            }else{
            	viewholder = (ViewHolder) vi.getTag();
            }
	            if(CallListHeader.get(position).getPointSection()==1){
	            	viewholder.tvDate.setText(""+ fDisplayDate(position, viewholder));
	            }else{
	            	 fDisplayNumberAndDate(position,viewholder);
	            }
            
            //ReCall
            final int id = position; //get id
            viewholder.btRecall.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					fRecall(id);
				}
			});
            return vi;
     }
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	/**
	 * Vew holder
	 * 
	 */
	static class ViewHolder {
		TextView tvDate ;
	    TextView tvNumber ;
	    ImageView ivCall ;
	    Button btRecall ;
	    RelativeLayout relativeLayout ;
	}
	/**
	 * Section date: ToDay
	 * 
	 * @param date
	 * @param formattedDate
	 */
	public int setDateToday(String date){
		Calendar c = Calendar.getInstance();
 		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 		String formattedDate = df.format(c.getTime());
 		 if(date.equals(formattedDate)){
 			 return 1;
 		 }
 		 return 0;
	}
	/**
	 * Section date: yesterday
	 * 
	 * @param date
	 * @param formattedDate
	 * @return
	 */
	public int setDateYesterday(String date){
		Calendar c = Calendar.getInstance();
 		c.add(Calendar.DATE, -1);
 		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 		String formattedDate = df.format(c.getTime());
 		if(date.equals(formattedDate)){
			 return 1;
		 }
// 		hoai
		 return 0;
	}
	/**
	 * recall phone number
	 * @param id
	 */
	private void fRecall(int id){
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		callIntent.setData(Uri.parse("tel:"+CallListHeader.get(id).getPhoneNumber()));
		context.startActivity(callIntent);
	}
	/**
	 * Create component
	 */
	private void fCreateComponent(View vi,ViewHolder view){
		// display date
		lparamDate = new LinearLayout.LayoutParams(0, 0);
		// display date and number
		lparamDateNumber = new LinearLayout.LayoutParams(
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);
		view.tvDate = (TextView) vi.findViewById(R.id.tvdateHistory);
		view.tvNumber = (TextView) vi.findViewById(R.id.tvnumberHistory);
		view.ivCall = (ImageView) vi.findViewById(R.id.ivCallHistory);
		view.btRecall = (Button) vi.findViewById(R.id.btReCallHistory);
		view.relativeLayout = (RelativeLayout) vi
				.findViewById(R.id.layoutCallHistory);
	}
	/**
	 * convert datetime to date
	 * @param date
	 */
	private String stringToDate(String day){
		Date date = null;
		SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
		try {  
		     date = format.parse(day);  
		    System.out.println(date);  
		} catch (ParseException e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		SimpleDateFormat  dateformat = new SimpleDateFormat("yyyy-MM-dd");  
		String day1=null;
		try {  
		  
		 day1= dateformat.format(date);
		} catch (Exception e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		return day1;
	}
	/**
	 * convert datetime to time
	 * @param time
	 * @return
	 */
	private String stringToTime(String time){
		Date date = null;
		SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
		try {  
		     date = format.parse(time);  
		    System.out.println(date);  
		} catch (ParseException e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		SimpleDateFormat  dateformat = new SimpleDateFormat("HH:mm");  
		String time1 =null;
		try {  
		  
		 time1= dateformat.format(date);
		} catch (Exception e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		return time1;
	}
	/**
	 * Display date
	 */
	private String fDisplayDate(int id, ViewHolder view){
		view.btRecall.setLayoutParams(lparamDate);
		view.tvNumber.setLayoutParams(lparamDate);
		view.ivCall.setLayoutParams(lparamDate);
		view.tvDate.setLayoutParams(lparamDateNumber);
		view.tvDate.setFocusable(true);
		view.relativeLayout.setBackgroundColor(Color.parseColor("#1d1e23"));
    	 String getdatetime =CallListHeader.get(id).getDateTime();
    	 if(setDateToday(stringToDate(getdatetime))==1){
    		 return "Today";
    	 }
    	 else if(setDateYesterday(stringToDate(getdatetime))==1){
    		 return "Yesterday";
    	 }
    	 else 
    	 return stringToDate(getdatetime);
	}
	/**
	 * Display phone number
	 * @param position
	 */
	private void fDisplayNumberAndDate(int position,ViewHolder view){
		view.btRecall.setLayoutParams(lparamDateNumber);
		view.btRecall.setLayoutParams(lparamDateNumber);
		view.ivCall.setLayoutParams(lparamDateNumber);
		view.tvNumber.setLayoutParams(lparamDateNumber);
		view.tvNumber.setText(""+CallListHeader.get(position).getPhoneNumber().toString());
		view.tvDate.setTextSize(10);
		view.tvDate.setFocusable(false);
		String getdatetime = CallListHeader.get(position).getDateTime();
		view.tvDate.setText("Time: "+stringToTime(getdatetime));
		view.relativeLayout.setBackgroundColor(Color.parseColor("#3d3f4b"));
		//
		//set model call
		//
		int model = CallListHeader.get(position).getStatus();
		switch (model) {
		case 16:
			view.ivCall.setBackgroundResource(R.drawable.modecall1);
			break;
		case 32:
			view.ivCall.setBackgroundResource(R.drawable.modecall2);
			break;
		case 48:
			view.ivCall.setBackgroundResource(R.drawable.modecall3);
			break;
		case 64:
			view.ivCall.setBackgroundResource(R.drawable.modecall4);
			break;
		case 80:
			view.ivCall.setBackgroundResource(R.drawable.modecall5);
			break;
		case 96:
			view.ivCall.setBackgroundResource(R.drawable.modecall6);
			break;

		default:
			break;
		}
		
	}


	public void update(ArrayList<HeaderSMSBlockedItem> callListHeader2) {
		// TODO Auto-generated method stub
		CallListHeader = callListHeader2;
		notifyDataSetChanged();
	}
}